<?php
/**
 * Created by PhpStorm .
 * User: 杜磊磊
 * Date: 2020/7/11
 * Time: 16:35
 */
namespace app\admin\controller;

use think\facade\View;
use app\common\business\Category as CategoryBus;
use app\common\lib\Status as StatusLib;
class Category extends AdminBase
{
    public function index()
    {
        $pid = input("param.id",0,'intval');
        $data = [
            'pid' => $pid,
        ];
        try {
            $categorys = (new CategoryBus())->getLists($data,5);
        }catch (\Exception $e){
            $categorys = [];
        }
        //halt($categorys);
        return  View::fetch('',[
            "categorys" => $categorys,
        ]);
    }

    public function add()
    {
        try {
            $categorys = (new CategoryBus())->getNormalCategory();
        }catch (\Exception $e){
            $categorys = [];
        }
        return  View::fetch("",[
            "categorys" => json_encode($categorys),
        ]);
    }

    /**
     * 新增数据
     * @return \think\response\Json
     */
    public function save()
    {
        $pid = input("param.pid","","intval");
        $name = input("param.name","","trim");
        $data = [
            "pid" => $pid,
            'name' => $name,
        ];
        //校验数据
        $validate = new \app\admin\validate\Category();
        if (!$validate->check($data)){
            return show(config("status.error"),$validate->getError());
        }
        try {
            $result = (new CategoryBus())->add($data);
        }catch (\Exception $e){
            return show(config("status.error"),$e->getMessage());
        }
        if($result){
            return show(config("status.success"),'添加成功');
        }
        return show(config("status.error"),'添加失败');

    }

    /**
     * 排序
     * @return \think\response\Json
     * Author: 杜磊磊
     * Date: 2020/9/24 9:23
     */
    public function listorder()
    {
        $id = input("param.id",0,'intval');
        $listorder = input("param.listorder",0,'intval');
        if(!$id){
            return show(config('status.error'),'参数错误');
        }
        try {
            $res = (new CategoryBus())->listorder($id,$listorder);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        if($res){
            return show(config('status.success'),'排序成功');
        }else{
            return show(config('status.error'),'排序失败');
        }
    }


    public function status()
    {
        $status = input('param.status',0,'intval');
        $id = input('param.id',0,'intval');
        if (!$id || !in_array($status,StatusLib::getTableStatus())){
            return show(config('status.error'),'参数错误');
        }
        try {
            $res = (new CategoryBus())->status($id,$status);
        }catch (\Exception $e){
            return show(config("status.error"),$e->getMessage());
        }
        if($res){
            return show(config("status.success"),"状态跟新成功");
        }else{
            return show(config("status.error"),"状态跟新失败");
        }
    }
}